package com.sgb.kjwl.view.widget.manage;

import android.content.Context;
import android.text.method.KeyListener;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;

import com.sgb.kjwl.R;

/**
 * 带省略号的edittext
 */
public class EllipsisEditText extends FrameLayout {
    private KeyListener mKeyListener;
    private Context context;
    private EditText et;
    public EllipsisEditText(Context context) {
        this(context, null);
    }

    public EllipsisEditText(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public EllipsisEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
        init();
    }

    private void init(){
        setFocusable(true);
        setFocusableInTouchMode(true);
        View view = LayoutInflater.from(context).inflate(R.layout.customer_ellipsis_edittxt, this);
        et = view.findViewById(R.id.et);
        mKeyListener = et.getKeyListener();
        et.setKeyListener(null);
        et.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus){
                    et.setKeyListener(mKeyListener);
                    String s = et.getText().toString();
                    et.setText(s);
                } else {
                    et.setKeyListener(null);
                    et.requestLayout();
                    et.postInvalidate();
                }
            }
        });
    }

    public void setHint(String hint){
        if (et != null){
            et.setHint(hint);
        }
    }

    public String getName(){
        if (et != null){
            return et.getText().toString();
        }
        return "";
    }

    public void setName(String name){
        if (et != null){
            et.setText(name);
        }

    }
}
